package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IFacebookDao;
import com.cn.wanxi.model.FacebookModel;
import com.cn.wanxi.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FacebookDaoImpl implements IFacebookDao {
    @Override
    public int deleteFacebook(int id) {
        String sql = "delete from facebook where id=" +id;
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<FacebookModel> getFacebookList() {
        String sql = "select * from facebook";
        List<FacebookModel> list = new ArrayList<>();
        FacebookModel facebook = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                facebook = new FacebookModel();
                facebook.setId(rs.getInt("id"));
                facebook.setTitle(rs.getString("title"));
                facebook.setEmail(rs.getString("email"));
                facebook.setContent(rs.getString("content"));
                facebook.setCreateTime(rs.getString("create_time"));
                facebook.setUpdateTime(rs.getString("update_time"));
                facebook.setEnabled(rs.getInt("enable"));
                facebook.setNote(rs.getString("note"));
                list.add(facebook);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public FacebookModel getFacebook(int id) {
        String sql = "select * from facebook where id = "+id;
        FacebookModel facebook = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                facebook = new FacebookModel();
                facebook.setId(rs.getInt("id"));
                facebook.setTitle(rs.getString("title"));
                facebook.setEmail(rs.getString("email"));
                facebook.setContent(rs.getString("content"));
                facebook.setCreateTime(rs.getString("create_time"));
                facebook.setUpdateTime(rs.getString("update_time"));
                facebook.setEnabled(rs.getInt("enable"));
                facebook.setNote(rs.getString("note"));
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return facebook;
    }

    @Override
    public List<FacebookModel> searchFacebook(String sql) {
        List<FacebookModel> list = new ArrayList<>();
        FacebookModel facebook = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                facebook = new FacebookModel();
                facebook.setId(rs.getInt("id"));
                facebook.setTitle(rs.getString("title"));
                facebook.setEmail(rs.getString("email"));
                facebook.setContent(rs.getString("content"));
                facebook.setCreateTime(rs.getString("create_time"));
                facebook.setUpdateTime(rs.getString("update_time"));
                facebook.setEnabled(rs.getInt("enable"));
                facebook.setNote(rs.getString("note"));
                list.add(facebook);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<FacebookModel> getFacebookList(int page, int size) {
        String sql = "select * from facebook limit "+ (page-1)*size +","+size;
        List<FacebookModel> list = new ArrayList<>();
        FacebookModel facebook = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                facebook = new FacebookModel();
                facebook.setId(rs.getInt("id"));
                facebook.setTitle(rs.getString("title"));
                facebook.setEmail(rs.getString("email"));
                facebook.setContent(rs.getString("content"));
                facebook.setCreateTime(rs.getString("create_time"));
                facebook.setUpdateTime(rs.getString("update_time"));
                facebook.setEnabled(rs.getInt("enable"));
                facebook.setNote(rs.getString("note"));
                list.add(facebook);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public List<FacebookModel> getFacebookList(int page, int size, String sort) {
        String sql = "select * from facebook order by "+ sort +" limit "+ (page-1)*size +","+size;
        List<FacebookModel> list = new ArrayList<>();
        FacebookModel facebook = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                facebook = new FacebookModel();
                facebook.setId(rs.getInt("id"));
                facebook.setTitle(rs.getString("title"));
                facebook.setEmail(rs.getString("email"));
                facebook.setContent(rs.getString("content"));
                facebook.setCreateTime(rs.getString("create_time"));
                facebook.setUpdateTime(rs.getString("update_time"));
                facebook.setEnabled(rs.getInt("enable"));
                facebook.setNote(rs.getString("note"));
                list.add(facebook);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public long getFacebookCount() {
        String sql = "select count(*) from facebook";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int addFacebook(FacebookModel facebookModel) {
        String sql = "insert into facebook (title,email,content,create_time) values('"+ facebookModel.getTitle()
                +"','"+ facebookModel.getEmail() +"','"+ facebookModel.getContent()
                +"','"+ facebookModel.getCreateTime() +"')";
        return JDBCUtils.excute(sql);
    }

}
